﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace UI
{
    public class UIFieldRunMapChatInfo : MonoBehaviour
    {
        public GameObject worldChatLabel;
        public UIScrollView scrollView;
        public UnfixedGrid grid;
        List<ChatInfo> infoList = new List<ChatInfo>();
        List<GameObject> chatGoList = new List<GameObject>();
        void InitChatItemList()
        {
            CleanList();
            if (Chat.ChatManager.Instance.fieldChatInfoQue.Count != 0)
            {
                foreach (var item in Chat.ChatManager.Instance.fieldChatInfoQue)
                {
                    if (infoList.Contains(item))
                    {
                        continue;
                    }
                    else
                    {
                        InitItem(item);
                        infoList.Add(item);
                    }
                    

                }
                grid.Reposition();
                scrollView.ResetPosition();
            }
        }
        void CleanList() 
        {
            foreach (var item in chatGoList)
            {
                Destroy(item);
            }
            infoList.Clear();
        }
        void InitItem(ChatInfo info)
        {
            GameObject go = Instantiate(worldChatLabel) as GameObject;
            go.transform.SetParent(grid.transform);
            go.transform.localScale = new Vector3(1, 1, 1);
            go.transform.localEulerAngles = new Vector3(0, 0, 0);
            string str = info.playerName + "：" + info.content;
            go.GetComponent<WorldChatItemLabel>().Init(str, info.playerId);
            worldChatLabel.GetComponent<UILabel>().text = str;
            grid.Add(go, go.GetComponent<WorldChatItemLabel>().SetHeight());
            chatGoList.Add(go);
        }
      
        void Update()
        {
            if (Chat.ChatManager.Instance.newFieldInfoOnField)
            {
                InitChatItemList();
                Chat.ChatManager.Instance.newFieldInfoOnField = false;
            }

        }

        public void OpenChatPage()
        {
            if (!CanOpt()) return;

            string options = "playerId=";
            options += "&type=5";
            options += "&heroWarId=";
            options += "&playerName=";
            PageManager.Instance.OpenPage("ChatPage", options);
        }
        public bool CanOpt()
        {
            var ai = Room.RoomManager.currentRoom.charactorManager.GetSelfCharactor().ai as Room.RCAIActor;
            return ai.curState == Room.PlayerAIState.Normal;
        }
    }
}